package com.store.app.activity;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.adapter.AddGoodsRVAdapter;
import com.store.app.bean.BitmapBean;
import com.store.app.bean.EventBusBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.Utils;
import com.store.app.widget.RecycleViewDivider;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/5/16.
 * 单品补货页面
 */
public class GoodReplenishmentActivity extends BaseActivity implements IOnHttpListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private TextView tv_zbm;                                                    //自编码
    private TextView tv_barcode;                                                //条形码
    private TextView tv_goods_name;                                             //名称
    private TextView tv_price;                                                  //售价
    private TextView tv_price_specifications;                                   //售价规格
    private TextView tv_purchase_price_specifications;                          //进价规格
    private TextView tv_unit;                                                   //单位
    private TextView tv_bzq;                                                    //保质期
    private TextView tv_pcm;                                                    //批次码
    private TextView tv_clw;                                                    //陈列位
    private TextView tv_cancel;                                                 //取消
    private TextView tv_save;                                                   //保存

    private EditText et_purchase_price;                                         //进价（必填）
    private EditText et_number;                                                 //数量（必填）
    private TextView tv_expiration_date;                                        //生产日期(选填)
    private EditText et_jxs;                                                    //经销商(选填）
    private RecyclerView rv_show_goods_pic;
    private AddGoodsRVAdapter mAddGoodsRVAdapter;
    private List<BitmapBean> mUploadBitmapBeans;//上传图片列表
    private NetRequest netRequest;
    private String stores_goods_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_replenishment);
        netRequest = new NetRequest(this);
        initView();
        initData();
    }

    private void initData() {
        String zbm = getIntent().getStringExtra("zbm");
        String barcode = getIntent().getStringExtra("barcode");
        String goods_name = getIntent().getStringExtra("goods_name");
        String price = getIntent().getStringExtra("price");
        String price_specifications = getIntent().getStringExtra("price_specifications");
        String purchase_price_specifications = getIntent().getStringExtra("purchase_price_specifications");
        String unit = getIntent().getStringExtra("unit");
        String bzq = getIntent().getStringExtra("bzq");
        String pcm = getIntent().getStringExtra("pcm");
        String clw = getIntent().getStringExtra("clw");
        stores_goods_id = getIntent().getStringExtra("stores_goods_id");
        String pic_detail_info_url = getIntent().getStringExtra("pic_detail_info_url");
        if (!TextUtils.isEmpty(zbm)) {
            tv_zbm.setText(zbm);
        }
        if (!TextUtils.isEmpty(barcode)) {
            tv_barcode.setText(barcode);
        }
        if (!TextUtils.isEmpty(goods_name)) {
            tv_goods_name.setText(goods_name);
        }
        if (!TextUtils.isEmpty(price)) {
            tv_price.setText(price);
        }
        if (!TextUtils.isEmpty(price_specifications)) {
            tv_price_specifications.setText(price_specifications);
        }
        if (!TextUtils.isEmpty(purchase_price_specifications)) {
            tv_purchase_price_specifications.setText(purchase_price_specifications);
        }
        if (!TextUtils.isEmpty(unit)) {
            tv_unit.setText(unit);
        }
        if (!TextUtils.isEmpty(bzq)) {
            tv_bzq.setText(bzq);
        }
        if (!TextUtils.isEmpty(pcm)) {
            tv_pcm.setText(pcm);
        }
        if (!TextUtils.isEmpty(clw)) {
            tv_clw.setText(clw);
        }

        try {
            JSONArray jsonArray = new JSONArray(pic_detail_info_url);
            mUploadBitmapBeans.clear();
            for (int i = 0; i < jsonArray.length(); i++) {
                BitmapBean bitmapBean = new BitmapBean();
                bitmapBean.setDoc_path(jsonArray.getJSONObject(i).getString("path"));
                mUploadBitmapBeans.add(bitmapBean);
            }
            mAddGoodsRVAdapter.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("单品补货");
        tv_zbm = (TextView) findViewById(R.id.tv_zbm);
        tv_barcode = (TextView) findViewById(R.id.tv_barcode);
        tv_goods_name = (TextView) findViewById(R.id.tv_goods_name);
        tv_price = (TextView) findViewById(R.id.tv_price);
        tv_price_specifications = (TextView) findViewById(R.id.tv_price_specifications);
        et_purchase_price = (EditText) findViewById(R.id.et_purchase_price);
        tv_purchase_price_specifications = (TextView) findViewById(R.id.tv_purchase_price_specifications);
        et_number = (EditText) findViewById(R.id.et_number);
        tv_unit = (TextView) findViewById(R.id.tv_unit);
        tv_expiration_date = (TextView) findViewById(R.id.tv_expiration_date);
        tv_bzq = (TextView) findViewById(R.id.tv_bzq);
        et_jxs = (EditText) findViewById(R.id.et_jxs);
        tv_pcm = (TextView) findViewById(R.id.tv_pcm);
        tv_clw = (TextView) findViewById(R.id.tv_clw);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_save = (TextView) findViewById(R.id.tv_save);
        tv_expiration_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePicker();
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });
        rv_show_goods_pic = (RecyclerView) findViewById(R.id.rv_show_goods_pic);
        initRecyclerView();
    }

    private void initRecyclerView() {
        mUploadBitmapBeans = new ArrayList<>();
        rv_show_goods_pic.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rv_show_goods_pic.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 10, Color.WHITE));
        mAddGoodsRVAdapter = new AddGoodsRVAdapter(this, mUploadBitmapBeans);
        rv_show_goods_pic.setAdapter(mAddGoodsRVAdapter);
    }

    private void submit() {
        String price = et_purchase_price.getText().toString().trim();
        if (TextUtils.isEmpty(price)) {
            Toast.makeText(this, "请输入进价!", Toast.LENGTH_SHORT).show();
            return;
        }

        String number = et_number.getText().toString().trim();
        if (TextUtils.isEmpty(number)) {
            Toast.makeText(this, "请输入数量!", Toast.LENGTH_SHORT).show();
            return;
        }
        showProgressDialog("", "正在补货...");
        netRequest.requestStockReplenish(1, number, price, tv_expiration_date.getText().toString().trim(), et_jxs.getText().toString().trim(), stores_goods_id);
    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            showToast("补货成功！");
            EventBus.getDefault().post(new EventBusBean(229));//通知GoodsActivity刷新数据
            finish();
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        showToast(msg);
    }

    private void showDatePicker() {
        Calendar mycalendar = Calendar.getInstance(Locale.getDefault());
        int year = mycalendar.get(Calendar.YEAR);
        int month = mycalendar.get(Calendar.MONTH);
        int day = mycalendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dpd = new DatePickerDialog(this, datelistener, year, month, day);
        dpd.show();//显示DatePickerDialog组件
    }

    private DatePickerDialog.OnDateSetListener datelistener = new DatePickerDialog.OnDateSetListener() {
        /**params：view：该事件关联的组件
         * params：myyear：当前选择的年
         * params：monthOfYear：当前选择的月
         * params：dayOfMonth：当前选择的日
         */
        @Override
        public void onDateSet(DatePicker view, int myyear, int monthOfYear, int dayOfMonth) {
            //修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
            //更新日期
            Calendar mycalendar = Calendar.getInstance();
            mycalendar.set(Calendar.YEAR, myyear);
            mycalendar.set(Calendar.MONTH, monthOfYear);
            mycalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            tv_expiration_date.setText(Utils.getTime(mycalendar));
        }
    };
}
